package com.dxy.app.widget.shrink_upvote;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import java.util.ArrayList;
import java.util.List;

/**
  *Created by duxingyu on 2017/7/21.16:48
  *Email: 
  *Description:好评表情
  **/

public class UpvoteLikeView extends ShrinkUpvoteView {

  /**
   * 特色画笔
   * */
  private Paint mouthPaint;
  /**
   * 轨迹列表
   * */
  private List<Float> list;
  /**
   * 表情初始偏移量
   * */
  private float emojDelY=0;
  /**
   * 绘制哈哈笑
   * */
  private float laughDelX;

  public UpvoteLikeView(Context context) {
    super(context);
  }

  public UpvoteLikeView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public UpvoteLikeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override protected void initSelfParams() {
    mouthPaint =new Paint();
    mouthPaint.setAntiAlias(true);
    mouthPaint.setStrokeWidth(eyeRadius/2);
    mouthPaint.setColor(colorBorder);
    mouthPaint.setStyle(Paint.Style.STROKE);
    mouthPaint.setStrokeCap(Paint.Cap.ROUND);

    list =new ArrayList<>();
    list.add(eyeToMiddleX);
    list.add(0-eyeToMiddleX);
    list.add(eyeToMiddleX);
    list.add(0-eyeToMiddleX);
    list.add(eyeToMiddleX);
    list.add(0-eyeToMiddleX);

    type =VoteType.LIKE;
  }

  @Override protected void drawFace(Canvas canvas,float offsetY) {
    float eyeDelY =positionY-radius - offsetY-emojDelY;

    mouthPaint.setColor(colorBorder);
    RectF rLeft =new RectF(positionX-eyeToMiddleX-2*eyeRadius,eyeDelY-eyeToTopY-laughDelX,positionX-eyeToMiddleX+eyeRadius,eyeDelY+eyeToTopY+laughDelX);
    canvas.drawArc(rLeft,210,120,false, mouthPaint);

    RectF rRight =new RectF(positionX+eyeToMiddleX-eyeRadius,eyeDelY-eyeToTopY-laughDelX,positionX+eyeToMiddleX+eyeRadius*2,eyeDelY+eyeToTopY+laughDelX);
    canvas.drawArc(rRight,210,120,false, mouthPaint);

    paint.setColor(colorBorder);
    RectF rMouth =new RectF(positionX-eyeToMiddleX*3/2-laughDelX,eyeDelY-eyeToMiddleX-laughDelX,positionX+eyeToMiddleX*3/2+laughDelX,eyeDelY+eyeToMiddleX*2+laughDelX);
    canvas.drawArc(rMouth,0,180,true,paint);
  }

  @Override protected void shrinkControl() {
    for(float each:list){
      if(emojDelY<each){//上升
        for(float k=emojDelY;k<each;k++){
          emojDelY+=1;
          laughDelX+=1/2f;
          if(emojDelY>=each){
            emojDelY =each;
            break;
          }
          postInvalidate();
          try {
            Thread.sleep(20);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }else{//下降
        for(float k=emojDelY;k>0;k--){
          emojDelY -=1;
          laughDelX-=1/2f;
          if(emojDelY<=0){
            emojDelY =0;
            laughDelX =0;//归零处理，避免出现误差的累加
            break;
          }
          postInvalidate();
          try {
            Thread.sleep(20);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
  }
}
